package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import com.example.mybatisplus.mapper.WhitelistSettingMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.WhitelistSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-10
 */
@Service
public class WhitelistSettingServiceImpl extends ServiceImpl<WhitelistSettingMapper, WhitelistSetting> implements WhitelistSettingService {
    @Autowired
    private WhitelistSettingMapper whitelistSettingMapper;
    @Override
    public WhitelistSetting login(WhitelistSetting whitelistSetting) {
        QueryWrapper<WhitelistSetting> wrapper = new QueryWrapper<>();
        wrapper.eq("sn",whitelistSetting.getSn())
                .eq("password",whitelistSetting.getPassword())
                .eq("is_enabled",true);
        WhitelistSetting one = whitelistSettingMapper.selectOne(wrapper);
        if(one != null){
            // 登录的时候把登录用户存到session
            SessionUtils.saveCurrentUser(one);
        }
        return one;
    }


    //以下两行代码等价
    //StringUtils.isNoneBlank(whitelistSetting.getSn())
    //whitelistSetting.getName()!=null && whitelistSetting.getName()!=""
    @Override
    public Page<WhitelistSetting> pageList(WhitelistSetting whitelistSetting, PageDTO dto) {
        QueryWrapper<WhitelistSetting> wrapper = new QueryWrapper<>();
        if(StringUtils.isNoneBlank(whitelistSetting.getSn())){
            wrapper.like("sn",whitelistSetting.getSn());
        }
        if(StringUtils.isNoneBlank(whitelistSetting.getName())){
            wrapper.like("name",whitelistSetting.getName());
        }
        if(Objects.nonNull(whitelistSetting.getRoleId())){
            wrapper.eq("role_id",whitelistSetting.getRoleId());
        }
        Page<WhitelistSetting> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPagesize());
        baseMapper.selectPage(page,wrapper);
        return page;

    }

    @Override
    public void exportUser(HttpServletResponse response)  {
        // 查询所有的用户
        List<WhitelistSetting> whitelistSettings = baseMapper.selectList(null);
        // xls
        // xlsx
        Workbook workbook = new HSSFWorkbook();
        // 创建工作表对象
        Sheet sheet = workbook.createSheet("第一个工作表");
        // 创建行对象
        Row row = sheet.createRow(0);
        // 创建行内单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("学号");

        cell = row.createCell(1);
        cell.setCellValue("姓名");

        cell = row.createCell(2);
        cell.setCellValue("密码");

        cell = row.createCell(3);
        cell.setCellValue("角色");

        for(int i=0;i<whitelistSettings.size();i++){
            row = sheet.createRow(i+1);
            cell = row.createCell(0);
            cell.setCellValue(whitelistSettings.get(i).getSn());

            cell = row.createCell(1);
            cell.setCellValue(whitelistSettings.get(i).getName());

            cell = row.createCell(2);
            cell.setCellValue(whitelistSettings.get(i).getPassword());

            cell = row.createCell(3);
            cell.setCellValue(whitelistSettings.get(i).getRoleId());
        }
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void importUser(MultipartFile file) throws IOException {
        // 验证文件 xlx xlsx
        // 根据后缀名获取对应poi对象
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        // 验证表头
        // 姓名 学号 角色
        // List<WhitelistSetting>
        // 判重---表格内部判重，表格内容和数据库的数据判重
        // 1. 不存在的数据（插入） 2.存在数据（修改） 告诉前端添加用户10人，
        // 2. 不存在的数据（插入） 2.存在的数据返回给前端
        // 3. 解析出来的数据全部返回给前端
        // poi单元格下拉
        // 我们假设importUser已经写好了，我现在完全没有思路，我都不知道这些参数有啥api我咋做啊？？？
    }
}
